package cn.itsource._02_work_queues;

import cn.itsource.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Rev {
    public static void main(String[] args) throws Exception {
        //建立连接对象
        Connection connection = ConnectionUtil.getConnection();
        //建立通道
        Channel channel = connection.createChannel();
        //交换机使用默认的
        //声明队列
        channel.queueDeclare(Send.NAME_WORK_QUEUES,true,false,false,null);

        //同时处理一个消息
        channel.basicQos(1);

        //接受消息的回调方法
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //模拟消费效率低
                try {
                    Thread.sleep(50000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //消息内容
                System.out.println("接受消息完成"+new String(body));
                //手动签收
                channel.basicAck(envelope.getDeliveryTag(),false);
                System.out.println("消费完成");
            }
        };
        //接受消息
        channel.basicConsume(Send.NAME_WORK_QUEUES, false, consumer);

        //自动接受，在接受数据时发生错误的话，可能会导致数据丢失
    }
}
